<?php
/**
 * Get the file requested and sends it
 * 
 * @author Sean
 * @return 
 */
include ("connect.php");
include ("config.php");

ini_set("memory_limit","200M");

$fn = (isset($_SERVER['HTTP_X_FILENAME']) ? $_SERVER['HTTP_X_FILENAME'] : false);

$GoogleID = $_GET['GoogleID'];
$GoogleID = mysql_real_escape_string($GoogleID);

$SessionID = $_GET['SessionID'];
$SessionID = mysql_real_escape_string($SessionID);

$result = mysql_query("SELECT sessionID FROM users WHERE googleID = '" . $GoogleID . "'");

while ($row = mysql_fetch_array($result)) {
   $DB_SessionID = $row["sessionID"];
}

if($SessionID == $DB_SessionID)
{
	if ($fn)
	{
		$fn = str_replace(' ', '_', $fn);
		$fn = str_replace('%', '_', $fn);
		
		$fn = mysql_real_escape_string($fn);
		$fn = str_replace('/', '_', $fn);
		$fn = str_replace("\"", '_', $fn);
		
		// AJAX call
		file_put_contents($pathToUsersFiles.$GoogleID."/".$fn, file_get_contents('php://input') );
		
		createThumbs($pathToUsersFiles.$GoogleID, 100);
		createGallery($pathToUsersFiles.$GoogleID, 400);
		
		exit();
	} 
	else {

		// form submit
		// $files = $_FILES['fileselect'];

		// foreach ($files['error'] as $id => $err) {
			// if ($err == UPLOAD_ERR_OK) {
				// $fn = $files['name'][$id];
				// move_uploaded_file(
					// $files['tmp_name'][$id],
					// "/var/www/webdrop/".$GoogleID."/".$fn
				// );
				// echo "<p>File $fn uploaded.</p>";
			// }
		// }
		// echo $GoogleID;
		// createThumbs("/var/www/webdrop/".$GoogleID, "/var/www/webdrop/".$GoogleID."/thumbs/",100);
		// createJson("/var/www/webdrop/".$GoogleID."/thumbs/", $GoogleID);
		// header('Location: http://127.0.0.1:8888/Webdrop.html?gwt.codesvr=127.0.0.1:9997#page=myFilePage');
		// header('Location: http://sean.servern.org/webdrop/Webdrop.html?#page=myFilePage');
	}
}
else{
	header("HTTP/1.0 404 Not Found");
	header('Location: http://sean.servern.org/webdrop/Webdrop.html?#page=Login');
}


/**
 * Creates a thumnail image if it is a image
 * 
 * @author Sean
 */
function createThumbs( $pathToImages, $thumbWidth )
{
	$pathToImages = $pathToImages."/";
	
	// open the directory
	$dir = opendir( $pathToImages );

  // loop through it, looking for any/all JPG files:
	while (false !== ($fname = readdir( $dir ))) {
		  
    // parse path for the extension
    $info = pathinfo($pathToImages . $fname);
	
		
    // continue only if this is a JPEG image
    if ( strtolower($info['extension']) == 'jpg' || 
		strtolower($info['extension']) == 'jpeg' || 
		strtolower($info['extension']) == 'png' || 
		strtolower($info['extension']) == 'gif' )
    {
		echo "Creating thumbnail for {$fname} <br />";

		// load image and get image size
		if(strtolower($info['extension']) == 'jpg' || strtolower($info['extension']) == 'jpeg' ){
			$img = imagecreatefromjpeg( "{$pathToImages}{$fname}" );
		}
		if(strtolower($info['extension']) == 'png'){
			$img = imagecreatefrompng( "{$pathToImages}{$fname}" );
		}
		if(strtolower($info['extension']) == 'gif'){
			$img = imagecreatefromgif( "{$pathToImages}{$fname}" );
		}
		$width = imagesx( $img );
		$height = imagesy( $img );

		// calculate thumbnail size
		$new_width = $thumbWidth;
		$new_height = floor( $height * ( $thumbWidth / $width ) );

		// create a new temporary image
		$tmp_img = imagecreatetruecolor( $new_width, $new_height );

		// copy and resize old image into new image
		imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );

		$fname = $info['dirname']."/thumbs/".$info['filename'].".".$info['extension'];

		// save thumbnail into a file
		
		if(strtolower($info['extension']) == 'jpg' || strtolower($info['extension']) == 'jpeg'){
			imagejpeg( $tmp_img, "{$fname}" );
		}
		if(strtolower($info['extension']) == 'png'){
			imagepng( $tmp_img, "{$fname}" );
		}
		if(strtolower($info['extension']) == 'gif'){
			imagegif( $tmp_img, "{$fname}" );
		}
		//destroys source and destination images. 
		imagedestroy($tmp_img); 
		imagedestroy($img); 
    }

  }
  // close the directory
  closedir( $dir );
}

/**
 * Creates a gallery image if it is a image
 * 
 * @author Sean
 */
function createGallery( $pathToImages, $thumbWidth )
{
	$pathToImages = $pathToImages."/";
	
	// open the directory
	$dir = opendir( $pathToImages );

	// loop through it, looking for any/all JPG files:
	while (false !== ($fname = readdir( $dir ))) {
		  
    // parse path for the extension
    $info = pathinfo($pathToImages . $fname);
	
		
    // continue only if this is a JPEG image
    if ( strtolower($info['extension']) == 'jpg' || 
		strtolower($info['extension']) == 'jpeg' || 
		strtolower($info['extension']) == 'png' || 
		strtolower($info['extension']) == 'gif' )
    {
		echo "Creating thumbnail for {$fname} <br />";

		// load image and get image size
		if(strtolower($info['extension']) == 'jpg' || strtolower($info['extension']) == 'jpeg' ){
			$img = imagecreatefromjpeg( "{$pathToImages}{$fname}" );
		}
		if(strtolower($info['extension']) == 'png'){
			$img = imagecreatefrompng( "{$pathToImages}{$fname}" );
		}
		if(strtolower($info['extension']) == 'gif'){
			$img = imagecreatefromgif( "{$pathToImages}{$fname}" );
		}
		$width = imagesx( $img );
		$height = imagesy( $img );

		// calculate thumbnail size
		$new_width = $thumbWidth;
		$new_height = floor( $height * ( $thumbWidth / $width ) );

		// create a new temporary image
		$tmp_img = imagecreatetruecolor( $new_width, $new_height );

		// copy and resize old image into new image
		imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );

		$fname = $info['dirname']."/gallery/".$info['filename'].".".$info['extension'];

		// save thumbnail into a file
		
		if(strtolower($info['extension']) == 'jpg' || strtolower($info['extension']) == 'jpeg'){
			imagejpeg( $tmp_img, "{$fname}" );
		}
		if(strtolower($info['extension']) == 'png'){
			imagepng( $tmp_img, "{$fname}" );
		}
		if(strtolower($info['extension']) == 'gif'){
			imagegif( $tmp_img, "{$fname}" );
		}
		//destroys source and destination images. 
		imagedestroy($tmp_img); 
		imagedestroy($img); 
    }


  }
  // close the directory
  closedir( $dir );
}

?>